<?php


namespace app\common\service\finance\merchant;


use app\model\AgentMerchantStat;
use app\model\AgentMerchantWithdraw;
use app\model\AgentMerchantWithdrawItem;
use app\model\AgentWithdraw;
use Carbon\Carbon;
use think\Exception;
use think\facade\Log;

class WithdrawSuccessService
{
    public function success($id)
    {

        $agentMerchantWithdraw = AgentMerchantWithdraw::find($id);

        if (!$agentMerchantWithdraw) {
            throw new Exception('不存在提现id');
        }

        if ($agentMerchantWithdraw->status == AgentMerchantWithdraw::STATUS_SUCCESS) {
            throw new Exception('已打款，请勿重复提交');
        }

        $agentMerchantWithdraw->status = AgentMerchantWithdraw::STATUS_SUCCESS;
        $agentMerchantWithdraw->save();


        AgentMerchantWithdrawItem::query()
            ->where('agent_merchant_withdraw_id', $id)
            ->select()->each(function (AgentMerchantWithdrawItem $agentMerchantWithdrawItem) {

                /**
                 * 处理统计状态
                 */
                if ($agentMerchantWithdrawItem->type == AgentMerchantWithdrawItem::TYPE_STAT) {

                    /**
                     * @var AgentMerchantStat $agentMerchantStat
                     */
                    $agentMerchantStat = $agentMerchantWithdrawItem->agentMerchantStat;

                    if (!$agentMerchantStat) {
                        Log::error('不存在提现明细');
                        throw new Exception('不存在提现明细');
                    }
                    if ($agentMerchantStat->status == AgentMerchantStat::STATUS_SUCCESS) {
                        Log::error('打款已成功请勿重复提交打款');
                        throw new Exception('打款已成功请勿重复提交打款');
                    }

                    $agentMerchantStat->status = AgentMerchantStat::STATUS_SUCCESS;
                    $agentMerchantStat->success_at = Carbon::now()->format('Y-m-d H:i:s');
                    $agentMerchantStat->save();
                }


                /**
                 * 处理提现
                 */
                if ($agentMerchantWithdrawItem->type == AgentMerchantWithdrawItem::TYPE_WITHDRAW) {

                    /**
                     * @var AgentWithdraw $agentWithdraw
                     */
                    $agentWithdraw = $agentMerchantWithdrawItem->agentWithdraw;

                    if ($agentWithdraw->status == AgentWithdraw::STATUS_SUCCESS) {
                        Log::error('提现已成功，请勿重复提现成功');
                        throw new Exception('提现已成功，请勿重复提现成功');
                    }

                    $agentWithdraw->status = AgentWithdraw::STATUS_SUCCESS;
                    $agentWithdraw->save();

                }

            });

        return $agentMerchantWithdraw;
    }


    /**
     * 已通过
     * @param $id
     * @return AgentMerchantWithdraw|array|mixed|\think\Model
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function pass($id)
    {

        $agentMerchantWithdraw = AgentMerchantWithdraw::find($id);

        if (!$agentMerchantWithdraw) {
            throw new Exception('不存在提现id');
        }

        if ($agentMerchantWithdraw->status != AgentMerchantWithdraw::STATUS_SUBMIT) {
            throw new Exception('状态非已提交');
        }

        $agentMerchantWithdraw->status = AgentMerchantWithdraw::STATUS_CONFIRM;
        $agentMerchantWithdraw->save();


        AgentMerchantWithdrawItem::query()
            ->where('agent_merchant_withdraw_id', $id)
            ->select()->each(function (AgentMerchantWithdrawItem $agentMerchantWithdrawItem) {

                /**
                 * 处理统计状态
                 */
                if ($agentMerchantWithdrawItem->type == AgentMerchantWithdrawItem::TYPE_STAT) {

                    /**
                     * @var AgentMerchantStat $agentMerchantStat
                     */
                    $agentMerchantStat = $agentMerchantWithdrawItem->agentMerchantStat;

                    if (!$agentMerchantStat) {
                        Log::error('不存在提现明细');
                        throw new Exception('不存在提现明细');
                    }
                    if ($agentMerchantStat->status == AgentMerchantStat::STATUS_WAIT_PAY) {
                        Log::error('已确认待打款');
                        throw new Exception('已确认待打款');
                    }

                    $agentMerchantStat->status = AgentMerchantStat::STATUS_WAIT_PAY;
                    $agentMerchantStat->success_at = Carbon::now()->format('Y-m-d H:i:s');
                    $agentMerchantStat->save();
                }


                /**
                 * 处理提现
                 */
                if ($agentMerchantWithdrawItem->type == AgentMerchantWithdrawItem::TYPE_WITHDRAW) {

                    /**
                     * @var AgentWithdraw $agentWithdraw
                     */
                    $agentWithdraw = $agentMerchantWithdrawItem->agentWithdraw;

                    if ($agentWithdraw->status == AgentWithdraw::STATUS_PASS) {
                        Log::error('已审核通过');
                        throw new Exception('已审核通过');
                    }

                    $agentWithdraw->status = AgentWithdraw::STATUS_PASS;
                    $agentWithdraw->save();

                }

            });

        return $agentMerchantWithdraw;
    }
}